<!--
 * @Description:
 * @Author: william
 * @Date: 2025-02-17 16:47:40
 * @LastEditors: william
 * @LastEditTime: 2025-04-28 14:13:29
-->
<template>
  <BaseDialog
    v-model="dialogVisible"
    :title="title"
    width="850px"
    :before-close="handleClose"
  >
    <el-form
      ref="refDialogForm"
      v-loading="formLoading"
      class="set-dialog-form"
      :label-width="
        language === 'en' ? '180px' : type == 'add' ? '80px' : '90px'
      "
      :model="formData"
      :rules="rules"
    >
      <el-row>
        <el-col v-if="type == 'add'" :span="24">
          <el-form-item :label="$t('hotel.customer')" prop="customerId">
            <SelectCustomer
              v-model="formData.customerId"
              type="add"
              :label="$t('hotel.customer')"
            />
          </el-form-item>
        </el-col>
        <el-col v-if="type == 'edit'" :span="12">
          <el-form-item :label="$t('fleet.customerName')">
            <TextInput
              v-model="formData.customerName"
              :placeholder="$t('fleet.customerName')"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col v-if="type === 'edit'" :span="12">
          <el-form-item
            :label="$t('fleet.customerContact')"
            :label-width="language === 'en' ? '180px' : '120px'"
            prop="customerContact"
          >
            <TextInput
              v-model="formData.customerContact"
              :label="$t('fleet.customerContact')"
            />
          </el-form-item>
        </el-col>
        <el-col v-if="type === 'edit'" :span="12">
          <el-form-item
            :label="$t('fleet.contactPhone')"
            prop="customerContactPhone"
          >
            <InputPhone
              v-model="formData.customerContactPhone"
              :label="$t('fleet.contactPhone')"
            />
          </el-form-item>
        </el-col>
        <el-col v-if="type === 'edit'" :span="12">
          <el-form-item
            :label="$t('fleet.customerEmail')"
            :label-width="language === 'en' ? '180px' : '120px'"
            prop="customerContactEmail"
          >
            <TextInput
              v-model="formData.customerContactEmail"
              :label="$t('fleet.customerEmail')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('ticket.ticketName')" prop="name">
            <TextInput
              v-model="formData.name"
              :placeholder="$t('ticket.ticketName')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('hotel.businessType')"
            :label-width="language === 'en' ? '180px' : '120px'"
            prop="businessType"
          >
            <el-select
              v-model="formData.businessType"
              :placeholder="
                g_rules.zhAndEnShowMsg(
                  g_t('public.pleaseSelect'),
                  $t('hotel.businessType')
                )
              "
            >
              <el-option
                v-for="item in g_options.ticketBusinessTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('hotel.bookingTime')" prop="bookTime">
            <DateTime
              v-model="formData.bookTime"
              style="width: 100%"
              :default-time="new Date(2025, 1, 1, 0, 0, 0)"
              :label="$t('hotel.bookingTime')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('hotel.personCount')"
            :label-width="language === 'en' ? '180px' : '120px'"
            prop="peopleCount"
          >
            <TextInput
              v-model="formData.peopleCount"
              verify-type="nzint"
              :label="$t('hotel.personCount')"
              :maxlength="4"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('hotel.amount')" prop="price">
            <TextInput
              v-model="formData.price"
              verify-type="float"
              :label="$t('hotel.amount')"
              @blur="handleInputBlur('price')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('fleet.costExpense')"
            :label-width="language === 'en' ? '180px' : '120px'"
            prop="costExpense"
          >
            <TextInput
              v-model="formData.costExpense"
              verify-type="float"
              :label="$t('fleet.costExpense')"
              @blur="handleInputBlur('costExpense')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('ticket.contactPerson')" prop="contactName">
            <TextInput
              v-model="formData.contactName"
              :label="$t('ticket.contactPerson')"
              :maxlength="32"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('ticket.contactPersonPhone')"
            :label-width="language === 'en' ? '180px' : '120px'"
            prop="contactPhone"
          >
            <InputPhone
              v-model="formData.contactPhone"
              :label="$t('ticket.contactPersonPhone')"
            />
          </el-form-item>
        </el-col>
        <el-col v-if="type === 'edit'" :span="12">
          <el-form-item
            :label="$t('fleet.emergencyContact')"
            prop="customerEmergencyContact"
          >
            <TextInput
              v-model="formData.customerEmergencyContact"
              :label="$t('fleet.emergencyContact')"
            />
          </el-form-item>
        </el-col>
        <el-col v-if="type === 'edit'" :span="12">
          <el-form-item
            :label="$t('fleet.emergencyContactPhone')"
            :label-width="language === 'en' ? '180px' : '120px'"
            prop="customerEmergencyContactPhone"
          >
            <InputPhone
              v-model="formData.customerEmergencyContactPhone"
              :label="$t('fleet.emergencyContactPhone')"
            />
          </el-form-item>
        </el-col>
        <!-- <el-col v-if="type === 'edit'" :span="12">
          <el-form-item :label="$t('fleet.orderStatus')" prop="status">
            <el-select
              v-model="formData.status"
              :placeholder="
                g_rules.zhAndEnShowMsg(
                  g_t('public.pleaseSelect'),
                  $t('fleet.orderStatus')
                )
              "
            >
              <el-option
                v-for="item in g_options.ticketOrderStatusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item :label="$t('public.remark')" prop="note">
            <TextInput
              v-model="formData.note"
              type="textarea"
              :rows="2"
              :label="$t('public.remark')"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="form-dialog-footer">
        <el-button plain @click="handleClose">{{
          $t('public.cancel')
        }}</el-button>
        <el-button type="primary" @click="handleSubmitForm">{{
          $t('public.confirm')
        }}</el-button>
      </div>
    </template>
  </BaseDialog>
</template>

<script setup>
const props = defineProps({
  type: {
    type: String,
    default: 'add' // add | edit
  },
  id: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['update'])
const dialogVisible = defineModel({
  type: Boolean,
  default: false
})
const { language } = storeToRefs(useAppStore())
const title = computed(() =>
  props.type === 'add' ? g_t('fleet.createOrder') : g_t('fleet.editOrder')
)

const refDialogForm = ref(null)
const formLoading = ref(false)
const formData = ref({
  customerId: '',
  name: '',
  businessType: '',
  bookTime: '',
  peopleCount: '',
  price: '',
  costExpense: '',
  contactName: '',
  contactPhone: '',
  note: ''
})
const rules = {
  customerId: g_rules.selectRule(g_t('hotel.customer')),
  customerContact: g_rules.commonInputText(g_t('fleet.customerContact')),
  customerContactPhone: g_rules.phone({
    label: g_t('fleet.contactPhone')
  }),
  customerContactEmail: g_rules.email({
    label: g_t('fleet.customerEmail'),
    required: false
  }),
  name: g_rules.commonInputText(g_t('ticket.ticketName')),
  businessType: g_rules.selectRule(g_t('hotel.businessType')),
  bookTime: g_rules.selectRule(g_t('hotel.bookingTime')),
  peopleCount: g_rules.personCount,
  price: g_rules.amount,
  costExpense: g_rules.costExpense,
  contactName: g_rules.commonInputText(g_t('ticket.contactPerson')),
  contactPhone: g_rules.phone({
    label: g_t('ticket.contactPersonPhone')
  }),
  customerEmergencyContactPhone: g_rules.phone({
    label: g_t('fleet.emergencyContactPhone'),
    required: false
  })
  // status: g_rules.selectRule(g_t('fleet.orderStatus'))
}

onMounted(() => {
  props.type === 'edit' && getFormData()
})

const getFormData = _ => {
  formLoading.value = true
  g_api.ticket
    .apiOrderDetail({ ticketId: props.id })
    .then(({ code, data }) => {
      if (code === '200' && data) {
        formData.value = {
          ...formData.value,
          id: '',
          status: ''
        }
        const customerEntity = {
          customerName: '',
          customerContact: '',
          customerContactPhone: '',
          customerContactEmail: '',
          customerEmergencyContact: '',
          customerEmergencyContactPhone: ''
        }
        g_utils.assignFormData(formData.value, data)
        if (data.customerEntity) {
          Object.keys(customerEntity).forEach(k => {
            customerEntity[k] = data.customerEntity[k]
              ? data.customerEntity[k] + ''
              : customerEntity[k]
          })
        }
        formData.value = {
          ...formData.value,
          ...customerEntity
        }
        console.log('formData.value', formData.value)
      }
    })
    .finally(() => {
      formLoading.value = false
    })
}

const handleClose = _ => {
  dialogVisible.value = false
}

const handleInputBlur = prop => {
  // 数字化处理
  refDialogForm.value.validateField([prop], vaild => {
    if (vaild && formData.value[prop]) {
      formData.value[prop] = formData.value[prop] * 1
    }
  })
}

const handleSubmitForm = () => {
  if (formLoading.value) return
  refDialogForm.value.validate(valid => {
    console.log('formData.value', formData.value)
    if (!valid) return
    formLoading.value = true
    const urlMethod =
      props.type === 'add'
        ? g_api.ticket.apiOrderCreate
        : g_api.ticket.apiOrderUpdate
    urlMethod(formData.value)
      .then(res => {
        if (res.code === '200') {
          ElMessage({
            message: res.message,
            type: 'success'
          })
          handleClose()
          emit('update')
        }
      })
      .finally(() => {
        formLoading.value = false
      })
  })
}
</script>

<style scoped lang="scss"></style>
